﻿using DayuCode.Designer.Common;
using DayuCode.Designer.Entity;
using DayuCode.Designer.Model;
using DayuCode.Designer.Service;
using Sunny.UI;
using System;
using System.Windows.Forms;

namespace DayuCode.Designer.Forms
{
    public partial class FormNewFunc : UIForm
    {
        FuncVO funcVO;
        long parentFuncId;
        long projectId;
        public FormNewFunc(long parentFuncId, long projectId, string parentFuncName)
        {
            InitializeComponent();
            
            dataTableControl.SetTable(0, projectId, 0);

            dataTableControl.OnSelectedTableChange += DataTableControl_OnSelectedTableChange;
            this.parentFuncId = parentFuncId;
            this.projectId = projectId;
            if(!string.IsNullOrWhiteSpace(parentFuncName))
            {
                this.Text = "功能定义      上级模块：[ " + parentFuncName + " ]";
            }
            this.funcVO = FuncHandler.getFuncVO(0, projectId);
            Func func = funcVO.Func;
           
            func = new Func();
            func.ParentId = parentFuncId;
            func.ProjectId = projectId;
            func.FuncType = 1;
            funcVO.Func = func;
           
            cmbJavaPkg.DataSource = funcVO.JavaPkgs;
            cmbWebPageLayoutId.DataSource = funcVO.WebPageLayouts;
            cmbAppPageLayoutId.DataSource = funcVO.AppPageLayouts;
            cmbPadPageLayoutId.DataSource = funcVO.PadPageLayouts;
            cmbConfigStatus.DataSource = funcVO.ConfigStatuses;

            txtName.Text = func.Name;
            txtFileName.Text = func.FileName;
            cmbJavaPkg.Text = func.JavaPkg;
            cmbWebPageLayoutId.SelectedItem = funcVO.WebPageLayouts.Find(item => item.Id == func.WebPageLayoutId);
            cmbAppPageLayoutId.SelectedItem = funcVO.AppPageLayouts.Find(item => item.Id == func.AppPageLayoutId);
            cmbPadPageLayoutId.SelectedItem = funcVO.PadPageLayouts.Find(item => item.Id == func.PadPageLayoutId);

            cmbConfigStatus.SelectedItem = funcVO.ConfigStatuses.Find(item => item.Code == func.ConfigStatus);

            txtWfCode.Text = func.WfCode;
            txtTips.Text = func.Tips;
            txtOrdnum.Text = "" + func.Ordnum;            
        }

        private void DataTableControl_OnSelectedTableChange(HDataTable table)
        {
            if (table == null) return;
            txtName.Text = table.TableCaption;
            
        }

        public Func Func
        {
            get
            {
                return funcVO.Func;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!dataTableControl.checkFormValid())
            {
                return;
            }

            Func func = this.funcVO.Func;
            string funName = txtName.Text.Trim();
            if (string.IsNullOrEmpty(funName))
            {
                ShowWarningTip("功能名称不能为空");
                return;
            }

            //string fileName = txtFileName.Text.Trim();
            //if (string.IsNullOrEmpty(fileName))
            //{
            //    ShowWarningTip("主页面文件名不能为空");
            //    return;
            //}

            string javaPkg = cmbJavaPkg.Text.Trim();
            if (string.IsNullOrEmpty(javaPkg))
            {
                ShowWarningTip("Java包名不能为空");
                return;
            }

            NameItem webPageLayout = cmbWebPageLayoutId.SelectedItem as NameItem;
            if (webPageLayout == null)
            {
                ShowWarningTip("PC页面布局不能为空");
                return;
            }

            NameItem appPageLayout = cmbAppPageLayoutId.SelectedItem as NameItem;
            NameItem padPageLayout = cmbPadPageLayoutId.SelectedItem as NameItem;
            CodeItem configStatus = cmbConfigStatus.SelectedItem as CodeItem;

            int ordnum = 0;

            string ordnumStr = txtOrdnum.Text.Trim();
            if (!string.IsNullOrEmpty(ordnumStr))
            {
                ordnum = int.Parse(ordnumStr);
            }

            func.Name = funName;
            //func.FileName = fileName;
            func.JavaPkg = javaPkg;
            func.WebPageLayoutId = webPageLayout.Id;
            func.AppPageLayoutId = appPageLayout != null ? appPageLayout.Id : null;
            func.PadPageLayoutId = padPageLayout != null ? padPageLayout.Id : null;
            func.ConfigStatus = configStatus != null ? configStatus.Code : "";
            func.WfCode = txtWfCode.Text.Trim();
            func.Tips = txtTips.Text.Trim();
            func.Ordnum = ordnum;

            func = FuncHandler.saveFunc(func);
            this.funcVO.Func = func;

            dataTableControl.SetFuncId(func.Id);
            Close();
            DialogResult = DialogResult.OK;
        }

        private void FormFunc_Load(object sender, EventArgs e)
        {

        }
    }
}
